package com.xnyzc.lhy.mis.controller.cmsapi.driver;

import com.xnyzc.lhy.common.component.config.security.TokenUser;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.common.util.PoiUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.cms.driver.DriverUserInfo;
import com.xnyzc.lhy.mis.entity.driver.wallet.OaDBandSupplierSeparateAccount;
import com.xnyzc.lhy.mis.entity.param.cms.RoleReceive;
import com.xnyzc.lhy.mis.entity.param.cms.driver.CmsOadWalletParam;
import com.xnyzc.lhy.mis.entity.result.cms.driver.CmsDriverComingListRes;
import com.xnyzc.lhy.mis.entity.result.cms.driver.CmsOaDriverWalletListRes;
import com.xnyzc.lhy.mis.entity.result.user.DriverUserResult;
import com.xnyzc.lhy.mis.entity.user.OaDUser;
import com.xnyzc.lhy.mis.entity.user.OaDUserInfo;
import com.xnyzc.lhy.mis.service.cms.driver.CmsDriverWalletManageService;
import com.xnyzc.lhy.mis.service.cms.util.IOaSysGetOfficeIdListService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author conglijuan
 * @date 2019-08-22 10:42
 */
@RestController
@RequestMapping("/cmsapi/driver/wallet")
public class CmsDriverWalletManageController {


    @Autowired
    private CmsDriverWalletManageService cmsDriverWalletManageService;

    @Autowired
    private IOaSysGetOfficeIdListService iOaSysGetOfficeIdListService;


    @ApiOperation(value = "查询司机财务历史转账批次记录")
    @PostMapping("getTransferHistoryRecordList")
    public Rv getTransferHistoryRecordList(){
        //token权限认证先不管
        return cmsDriverWalletManageService.getTransferHistoryRecordList();
    }

    @ApiOperation(value = "根据批次号分页查询数据")
    @PostMapping(value = "getBatchAmountsByBatchId",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Rv selectTansferAmountByBatchId( @ApiParam(name="batchNo",value = "批次号")@RequestParam("batchNo") Long batchNo,
                                            @ApiParam(name = "current",value = "当前页") @RequestParam("current") Integer current,
                                           @ApiParam(name = "pageSize",value = "当前页条数") @RequestParam("pageSize") Integer pageSize){
        return cmsDriverWalletManageService.selectTransferAmountByBatchId(batchNo,current,pageSize);
    }

    @ApiOperation(value = "司机财务转账文件上传",notes = "单文件上传")
    @PostMapping(value="uploadDriverAccounts",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Rv uploadDriverAccounts(@ApiParam(name = "transferFile",value = "文件") @RequestParam("transferFile") MultipartFile file,
                                   @ApiParam(name="batchNo",value = "批次号")@RequestParam("batchNo") Long batchNo,
                                   @ApiParam(name = "ttlamt",value = "总金额") @RequestParam("ttlamt") BigDecimal ttlamt

    ){
        long sysUserId = TokenUtil.getTokenUserId();
        /*String tag = TokenUtil.getTag();
        String clientId = TokenUtil.getClientId();
        if (CheckUtil.objIsEmpty(tag, clientId)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        if(CheckUtil.objIsEmpty(file) || CheckUtil.objIsEmpty(fileType)){
            throw PangException.create(EErrorCode.missingArg);
        }*/
        return cmsDriverWalletManageService.uploadDriverAccountExcel(sysUserId, ttlamt,file,batchNo);
    }


    @PostMapping(value = "/exportGetSettlementData")
    public void exportGetSettlementData(@RequestBody CmsOaDriverWalletListRes cmsOaDriverWalletListRes) {
        long sysUserId = TokenUtil.getTokenUserId();
        String tag = TokenUtil.getTag();
        String clientId = TokenUtil.getClientId();
        String startDat = cmsOaDriverWalletListRes.getStartDat();
        String endDat = cmsOaDriverWalletListRes.getEndDat();
        if (CheckUtil.objIsEmpty(tag, clientId,startDat,endDat)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        ServletRequestAttributes servletRequestAttributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        SXSSFWorkbook workbook = cmsDriverWalletManageService.exportDriverSettlementAccountData(cmsOaDriverWalletListRes);
        //以流输出到浏览器
        PoiUtil.writeToResponse(workbook, request, response, "DriverSettlementAccountData");
    }


    /**
     * 获取查询条件中供应商信息
     */
    @ApiOperation(value="根据登录角色查询供应商信息")
    @PostMapping(value = "/getAllOfficeName")
    public Rv getOfficeNameList(){
        long userId = TokenUtil.getTokenUserId();
        List<RoleReceive> roleReceives = iOaSysGetOfficeIdListService.selectOfficeNameList(userId);
       return Rv.wrap(roleReceives);
    }

    /***
    *
    *跟新账户信息
    *@author:immor
    *@date:2021/5/20
    */
     @ApiOperation(value = "跟新账户信息")
     @PostMapping(value = "/updateDriverAccount")
     public Rv updateDriverAccount(@RequestBody OaDUserInfo oaDUserInfo){
         long sysUserId = TokenUtil.getTokenUserId();
         String tag = TokenUtil.getTag();
         String clientId = TokenUtil.getClientId();
         if (CheckUtil.objIsEmpty(tag, clientId,oaDUserInfo)){
             throw PangException.create(EErrorCode.missingAuthArgs);
         }
       return  cmsDriverWalletManageService.updateDriverAccountByDriver(oaDUserInfo,sysUserId);
     }

    /**
     * 获取司机钱包列表
     *
     * @return
     */
    @ApiOperation(value = "司机钱包信息管理列表", notes = "司机钱包信息管理列表")
    @PostMapping(value = "/getCmsDriverWalletList")
    public Rv getCmsDriverWalletList(@RequestBody CmsOaDriverWalletListRes cmsOaDriverWalletListRes) {
        return cmsDriverWalletManageService.getCmsDriverWalletList(cmsOaDriverWalletListRes);
    }

    /**
     * 获取该司机的对应的绑定模式规则和查看自己绑定分账规则没
     */
    @ApiOperation(value = "获取该司机的对应的绑定模式规则", notes = "获取该司机的对应的绑定模式规则")
    @PostMapping(value = "/getDriverBandSupplierSeparateAccountList")
    public Rv getDriverBandSupplierSeparateAccountList(@RequestBody CmsOaDriverWalletListRes cmsOaDriverWalletListRes) {
        return cmsDriverWalletManageService.getDriverBandSupplierSeparateAccountList(cmsOaDriverWalletListRes);
    }

    /**
     *  司机绑定对应供应商的分账规则
     */
    @ApiOperation(value = "司机绑定对应供应商的分账规则", notes = "司机绑定对应供应商的分账规则")
    @PostMapping(value = "/driverBandSupplierSeparateAccount")
    public Rv driverBandSupplierSeparateAccount(@RequestBody List<OaDBandSupplierSeparateAccount> list) {
        return cmsDriverWalletManageService.driverBandSupplierSeparateAccount(list);
    }

    /**
     * 导出表格信息
     * @return
     */
    @PostMapping(value = "/exportGetWallet")
    public Rv exportGetWallet(@RequestBody CmsOaDriverWalletListRes cmsOaDriverWalletListRes) {
        String tag = TokenUtil.getTag();
        String clientId = TokenUtil.getClientId();
        if (CheckUtil.objIsEmpty(tag, clientId)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        return cmsDriverWalletManageService.exportGetWallet(cmsOaDriverWalletListRes);
    }

    /**
     * 详情页面中表格信息
     * @return
     */
    @PostMapping(value = "/getCmsDriverWalletDetailList")
    public Rv getCmsDriverWalletDetailList(@RequestBody CmsOaDriverWalletListRes cmsOaDriverWalletListRes) {
        String tag = TokenUtil.getTag();
        String clientId = TokenUtil.getClientId();
        if (CheckUtil.objIsEmpty(tag, clientId)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        return cmsDriverWalletManageService.getCmsDriverWalletDetailList(cmsOaDriverWalletListRes);
    }

    /**
     * 导出详情页面中表格信息
     * @return
     */
    @PostMapping(value = "/exportGetWalletDetail")
    public Rv exportGetWalletDetail(@RequestBody CmsOaDriverWalletListRes cmsOaDriverWalletListRes) {
        return cmsDriverWalletManageService.exportGetWalletDetail(cmsOaDriverWalletListRes);
    }


    /**
     * 导出司机账户详情页面中表格信息
     * @return
     */
    @PostMapping(value = "/exportDriverAccountDetail")
    public Rv exportDriverAccountDetail(@RequestBody CmsOaDriverWalletListRes cmsOaDriverWalletListRes) {
        String tag = TokenUtil.getTag();
        String clientId = TokenUtil.getClientId();
        if (CheckUtil.objIsEmpty(tag, clientId)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        ServletRequestAttributes servletRequestAttributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        SXSSFWorkbook workbook = cmsDriverWalletManageService.exportDriverAccountDetail(cmsOaDriverWalletListRes);
        //以流输出到浏览器
        PoiUtil.writeToResponse(workbook, request, response, "DriverAccount");
        return Rv.wrap();
    }

    /**
     * 导入司机账户信息
     */
    @ApiOperation(value = "司机账户文件上传",notes = "单文件上传")
    @PostMapping(value="importGetWallet",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Rv importGetWallet(@ApiParam(name = "file",value = "文件") @RequestPart("file") MultipartFile file,
                              @ApiParam(name="fileType",value = "文件类型")@RequestParam("fileType") String fileType){
        long sysUserId = TokenUtil.getTokenUserId();
        String tag = TokenUtil.getTag();
        String clientId = TokenUtil.getClientId();
        if (CheckUtil.objIsEmpty(tag, clientId)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        if(CheckUtil.objIsEmpty(file) || CheckUtil.objIsEmpty(fileType)){
            throw PangException.create(EErrorCode.missingArg);
        }

        return Rv.wrap(cmsDriverWalletManageService.saveExcelFileContent(sysUserId, tag, clientId,file));
    }

    /**
     * 司机账户结算数据
     */
    @ApiOperation(value="司机账户结算数据",notes = "settlementAccountData")
    @PostMapping(value = "/getSettlementAccountData")
    public Rv getSettlementAccountData(@RequestBody CmsOaDriverWalletListRes cmsOaDriverWalletListRes){
        //token验证
        long sysUserId = TokenUtil.getTokenUserId();
        String tag = TokenUtil.getTag();
        String clientId = TokenUtil.getClientId();
        if(CheckUtil.objIsEmpty(tag,clientId)){
            throw PangException.create(EErrorCode.missingArg);
        }
        return  cmsDriverWalletManageService.getSettlementAccountData(cmsOaDriverWalletListRes);
    }

}
